<?php

namespace App\Models\Entity\Column;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Class Recommend
 * @package App\Models\Entity\Column
 * @version August 1, 2017, 4:33 pm UTC
 */
class Recommend extends Model
{
    use SoftDeletes;

    public $table = 'recommends';

    protected $dates = ['deleted_at'];

    public $fillable = [
        // 'recommend_no',
        'name',
        'title',
        'subtitle',
        'collist_id',
        'column_id',
        'img',
        'short_desc',
        'long_desc',
        'created_at',
        'updated_at',

        // 上下线, 发布
        'online_status',
        'moderate_status',
        'publish_status',
        'moderate_by',
        'moderate_at',
        'online_at',
        'offline_at',
        'user_id',
        'publish_down_status',
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        // 'recommend_no' => 'string',
        'name'       => 'string',
        'title'      => 'string',
        'subtitle'   => 'string',
        'collist_id' => 'integer',
        'column_id'  => 'integer',
        'img'        => 'string',
        'short_desc' => 'string',
        'long_desc'  => 'string',
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [
        // 'recommend_no' => 'required|max:100|unique:recommends,recommend_no',
        'name'       => 'required|max:100',
        'title'      => 'max:100',
        'subtitle'   => 'max:100',
        'collist_id' => 'required|max:100',
        'column_id'  => 'required,max:100',
        'img'        => 'required|max:200',
        'short_desc' => 'required|max:100',
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     **/
    public function collist()
    {
        return $this->hasOne(\App\Models\Entity\Column\Collist::class, 'id', 'collist_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     **/
    public function column()
    {
        return $this->hasOne(\App\Models\Entity\Column\Column::class, 'id', 'column_id');
    }

    public function vassetis()
    {
        return $this->belongsToMany(\App\Models\Entity\Asset\Video\Vasseti::class, 'recommend_vassetis', 'recommend_id', 'vasseti_id');
    }

    /**
     * 关键推荐位数据
     * @return [type] [description]
     */
    public function recommenddata()
    {
        return $this->hasMany(\App\Models\Entity\Column\RecommendData::class, 'recommend_id', 'id');
    }
    /**
     * 过滤上下线数据
     **/
    public function scopeOnlineStatus($query, $status = 1)
    {
        return $query->where('online_status', $status);
    }

    /**
     * 过滤上线发布数据
     **/
    public function scopeOnlinePublished($query, $status = 1)
    {
        return $query->where(['online_status' => 1, 'publish_status' => $status]);
    }

    /**
     * 过滤下线发布数据
     **/
    public function scopeOfflinePublished($query, $status = 1)
    {
        return $query->where(['online_status' => 0, 'publish_down_status' => $status]);
    }
}
